<?php

class Menus
{
    protected $_modelName = 'Menu';
    protected $_data;

    function __construct()
    {
        $count=101;
        // number, menu name, item name, action name, allow role, deny role
        $this->_data = Array(
            Array('id'=>$count++ ,'menu'=>'總管理摘要' ,'menu_item'=>'Home'               ,'action'=>'admin/index'                          ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>true    ),
            Array('id'=>$count++ ,'menu'=>'總管理摘要' ,'menu_item'=>'我的服務管理'       ,'action'=>'adminUserServiceCategory/list'        ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
//          Array('id'=>$count++ ,'menu'=>'外觀管理'   ,'menu_item'=>'外觀管理Home'       ,'action'=>'admin/index2'                         ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'全站管理'   ,'menu_item'=>'客戶管理'           ,'action'=>'adminSiteClient/list'                 ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'全站管理'   ,'menu_item'=>'帳號管理'           ,'action'=>'adminSiteAccount/list'                ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'全站管理'   ,'menu_item'=>'服務管理'           ,'action'=>'adminSiteUserServiceCategory/list'    ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'全站管理'   ,'menu_item'=>'權限管理'           ,'action'=>'adminSiteUserPermissionCategory/list' ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'全站管理'   ,'menu_item'=>'客戶訂單管理'       ,'action'=>'adminSiteConsumerOrder/list'          ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),


//          Array('id'=>$count++ ,'menu'=>'全站管理'   ,'menu_item'=>'群組服務管理'       ,'action'=>'adminUserGroup/list'                  ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
//          Array('id'=>$count++ ,'menu'=>'帳號管理'   ,'menu_item'=>'檢閱'               ,'action'=>'adminAccount/view'                    ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
//          Array('id'=>$count++ ,'menu'=>'個人管理'   ,'menu_item'=>'我的帳號資訊'       ,'action'=>'????/edit'                            ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
//          Array('id'=>$count++ ,'menu'=>'全域設定'   ,'menu_item'=>'全域設定Home'       ,'action'=>'admin/index2'                         ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'網站管理'   ,'menu_item'=>'網站設定'           ,'action'=>'adminBlog/setting'                    ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'網站管理'   ,'menu_item'=>'文章管理'           ,'action'=>'adminBlogArticle/list'                ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'網站管理'   ,'menu_item'=>'文章分類管理'       ,'action'=>'adminBlogArticleCategory/list'        ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
//          Array('id'=>$count++ ,'menu'=>'網站管理'   ,'menu_item'=>'迴響管理'           ,'action'=>'adminBlogArticleComment/list'         ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),

            Array('id'=>$count++ ,'menu'=>'檔案管理'   ,'menu_item'=>'檔案管理'           ,'action'=>'adminGallaryAlbum/list'               ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
            Array('id'=>$count++ ,'menu'=>'登出'       ,'menu_item'=>'登出'               ,'action'=>'member/logout'                        ,'allow_role'=>''  ,'deny_role'=>'' ,'focus'=>false   ),
        );
    }

    public function mapRow($row)
    {
        $model = new $this->_modelName();
        $model->setId(          $row['id']          );
        $model->setMenu(        $row["menu"]        );
        $model->setMenuItem(    $row['menu_item']   );
        $model->setAction(      $row['action']      );
        $model->setAllowRole(   $row['allow_role']  );
        $model->setDenyRole(    $row['deny_role']   );
        $model->setFocus(       $row['focus']       );
        return $model;
    }

    // ================================================================================
    // add update delete
    // ================================================================================

    /**
     *  設定焦點 menu and menu item
     *  如果完全沒有, 就設定成 default 
     *   
     */
    public function setFocusByAction( $action ) 
    {
        $action = trim($action);

        $isSetFocus = false;
        foreach( $this->_data as $key => $data ) {
            if( $this->_data[$key]['action'] == $action ) {
                $this->_data[$key]['focus']=true;
                $isSetFocus = true;
            }
            else {
                $this->_data[$key]['focus']=false;
            }
        }

        if( !$isSetFocus ) {
            $this->_data[0]['focus']=true;
        }
        
    }

    public function setFocusByMenuAndMenuItem( $menuName, $menuItemName ) 
    {
        foreach( $this->_data as $key => $data ) {
            if( $this->_data[$key]['menu']      == $menuName     &&
                $this->_data[$key]['menu_item'] == $menuItemName    
            )
            {
                $this->_data[$key]['focus']=true;
            }
            else {
                $this->_data[$key]['focus']=false;
            }
        }
    }

    // ================================================================================
    // get
    // ================================================================================

    /**
     *  取得 focus 的 menu
     *  @param  bool            $coerce - 就算所有的 menu 都沒有 focus, 也要強迫中獎
     *  @return objects/bool     
     *  @access public
     */
    public function getMenuByFocus( $coerce=true ) 
    {
        foreach( $this->_data as $data ) {
            $menu = $this->mapRow($data);
            if($menu->isFocus()) {
                return $menu;
            }
        }

        //強迫中獎
        if($coerce) {
            $menu = $this->mapRow($this->_data[0]);
            return $menu;
        }

        return false;
    }

    /**
     *  只取得所有主選單 model 
     *  其中的 focus主選單 要正確取得, 不要取得 非focus 的主選單
     *
     *  @return objects
     *  @access public
     */
    public function getMenusByMasterMenu() 
    {
        $focusMenu = $this->getMenuByFocus();
        $focusMenuKey = md5($focusMenu->getMenu());

        $models = Array();
        foreach( $this->_data as $data ) {
            $menu = $this->mapRow($data);
            $key = md5($menu->getMenu());
            if( !isset($models[$key]) ) {
                if( $focusMenuKey==$key && $menu->getId()!=$focusMenu->getId() ) {
                    // 相同於主單選, 但非 focus 的就不處理
                }
                else {
                    $models[$key] = $menu;
                }
            }
        }
        $models = array_values($models);
        return $models;

    }

    // 只取得該 主選單 下的 子選單 model 
    public function getMenusByMasterMenuId( $id ) 
    {
        $key = 0;
        foreach( $this->_data as $data ) {
            $menu = $this->mapRow($data);
            if($menu->getId()==$id) {
                $key = $menu->getMenu();
                break;
            }
        }
        if(!$key) {
            return false;
        }

        $models = Array();
        foreach( $this->_data as $data ) {
            $menu = $this->mapRow($data);
            if( $menu->getMenu()==$key ) {
                $models[] = $menu;
            }
        }
        return $models;
    }

    // 取得所有的 選單 model 
    public function getAllMenus() 
    {
        $models = Array();
        foreach( $this->_data as $menu ) {
            $models[] = $this->mapRow($menu);
        }
        return $models;
    }


}

